<template>
    <header>
        <img class="logo" src="@/assets/logo.svg" width="125" height="125"/>
        <div class="wrapper">
            <Hello msg = "用户注册"/>
        </div>
    </header>
    <van-form @submit="onSubmit" @failed="onFailed">
        <van-cell-group>
            <van-field required v-model="user.phoneNum" name="手机号" label="手机号" placeholder="请输入手机号"
                :rules="[{ required: true, message: '请填写用户名' },{validator:isMobile,message:'手机号格式不正确！'}]"
            />
            <van-field required v-model="user.verifyCode" center clearable name="图片验证码"
                label="图片验证码" placeholder="请输入图片验证码"
                :rules="[{ required: true, message: '请填写图片验证码' }]"
            >
                <template #right-icon>
                    <!-- <van-image width="80" height="30" src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" /> -->
                    <van-image width="80" height="30" :src="kaptchaSrc" />
                </template>
            </van-field>
            <van-field required v-model="user.smsCode" center clearable name="短信验证码"
                label="短信验证码" placeholder="请输入短信验证码"
                :rules="[{ required: true, message: '请填写短信验证码' }]"
            >
                <template #button>
                    <van-button size="small" type="primary" >发送验证码</van-button>
                </template>
            </van-field>
            <van-field required v-model="user.userName" name="真实姓名" label="真实姓名" placeholder="真实姓名"
                :rules="[{ required: true, message: '真实姓名' }]"
            />
            <van-field  required v-model="user.password" type="password" name="密码" label="密码" placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field required v-model="user.repassword" type="password" name="确认密码" label="确认密码" placeholder="确认密码"
                :rules="[{ required: true, message: '请填写确认密码' },,{validator:isRePassword,message:'两次密码不一致！'}]"
            />
            <van-checkbox v-model="checked" icon-size="24px" style="margin: 10px; padding-left: 10px;">
                阅读《用户协议与隐私政策》
            </van-checkbox>
            <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
            </div>
        </van-cell-group>
 
 
</van-form>
</template>
<script setup>
import { ref,reactive } from 'vue';
import Hello from '../components/HelloWorld.vue'
import{ onMounted } from 'vue';
import{ isMobile } from '../utils/validate.js'
//import axios from 'axios'
import http from '../utils/request.js'
const checked = ref(false);
const kaptchaSrc = ref('');

const flushKaptcha = async function(){
    console.log('刷新图片验证码....')
  // const res  = await fetch('http:localhost:8080/user/kaptcha')
   //console.log((await res.json()));
   // kaptchaSrc.value = "data:image/jpg;base64," + (await res.json()).data.img;
   http.get("/user/kaptcha")
   .then((res) => {
    console.log(res)
    kaptchaSrc.value = "data:image/jpg;base64,"+ res.data.data.img;
   }).catch((err) => {
    console.log(err)
   })
}
const user = reactive({
    phoneNum: '',
    verifyCode: '',
    smsCode: '',
    userName: '',
    password: '',
    repassword: '',
})

function isRePassword(val, b){
    console.log('isRepassword',val,b);
    console.log(user.password, typeof(user.password),typeof(val));
    if(user.password === val){
        return true;
    }else{
        return false;
    }
}
const onFailed = function(values,errors){
    console.log('注册用户校验失败:',values,errors);
}
const onSubmit = function(){
    console.log('注册用户',user);
}

onMounted(() =>{
    flushKaptcha();
});
</script>
<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>
